package com.simen.downloader;

import android.os.Parcelable;

import com.simen.downloader.model.Filer;
import com.simen.downloader.util.Constants;
import com.simen.downloader.util.Utils;

import java.io.Serializable;
import java.net.URL;

/**
 * Created by zhangming on 16/5/12.
 */
final class StorageFile implements Serializable {

    public final static long serialVersionUID = 1l;

    private String fileUrl;
    private String fileRealUrl;
    private long current, total;
    private int progress;
    private long lasttime, createtime;
    private int status; //任务完成状态
    private String fileName, localPath;

    StorageFile() {

    }

    public StorageFile(String fileUrl, String fileRealUrl, long createtime,
                       long lasttime, long current, long total, int progress, String fileName,
                       String localPath) {
        this.createtime = createtime;
        this.current = current;
        this.fileName = fileName;
        this.fileRealUrl = fileRealUrl;
        this.fileUrl = fileUrl;
        this.lasttime = lasttime;
        this.progress = progress;
        this.total = total;
        this.localPath = localPath;
    }

    public StorageFile(String fileUrl, int status) {
        this.fileUrl = fileUrl;
        this.status = status;
    }

    public long getCreatetime() {
        return createtime;
    }

    public void setCreatetime(long createtime) {
        this.createtime = createtime;
    }

    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public String getExt() {
        return Utils.getExtensionName(fileName);
    }

    public String getFileRealUrl() {
        return fileRealUrl;
    }

    public void setFileRealUrl(String fileRealUrl) {
        this.fileRealUrl = fileRealUrl;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public long getLasttime() {
        return lasttime;
    }

    public void setLasttime(long lasttime) {
        this.lasttime = lasttime;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    boolean isCleared() {
        if (status == Constants.WORKCLEARED) {
            return true;
        }
        return false;
    }

    public boolean isEqualFileUrl(URL finalUrl) {
        if (finalUrl == null || this.fileUrl == null) {
            return false;
        }

        if (this.fileUrl.equals(finalUrl.toString())) {
            return true;
        }

        return false;
    }

    public boolean isCompleted() {
        return current == total;
    }

    public void clone(ParcelFile parcelFile) {
        this.createtime = parcelFile.getCreatetime();
        this.current = parcelFile.getCurrent();
        this.fileName = parcelFile.getFileName();
        this.fileRealUrl = parcelFile.getFileRealUrl();
        this.fileUrl = parcelFile.getFileUrl();
        this.lasttime = parcelFile.getLasttime();
        this.progress = parcelFile.getProgress();
        this.total = parcelFile.getTotal();
        this.localPath = parcelFile.getLocalPath();
    }

    @Override
    public String toString() {
        return "StorageFile{" +
                "createtime=" + createtime +
                ", fileUrl='" + fileUrl + '\'' +
                ", fileRealUrl='" + fileRealUrl + '\'' +
                ", current=" + current +
                ", total=" + total +
                ", progress=" + progress +
                ", lasttime=" + lasttime +
                ", status=" + status +
                ", fileName='" + fileName + '\'' +
                ", localPath='" + localPath + '\'' +
                '}';
    }

    public void setParamers(String key, String fileRealUrl, long createtime, long lasttime, long current, long total, int progress, String fileName, String filePath) {
        this.createtime = createtime;
        this.current = current;
        this.fileName = fileName;
        this.fileRealUrl = fileRealUrl;
        this.fileUrl = key;
        this.lasttime = lasttime;
        this.progress = progress;
        this.total = total;
        this.localPath = filePath;
    }
}
